const Web3 = require('web3')
const Token = require('../Token.json')

const ganachePrivateKey =
  '0x700c472fef576757270df5068e0ec7152e7307000cfe3fbf5d15be43bde55230'
const userAddress = '0xac9199717985cDaC98e5832370B208B8a33A50Ef'

// 连接provider
let local_provider = new Web3.providers.HttpProvider('http://localhost:8545')

// 构造web3
var web3 = new Web3(local_provider)

// 账户
let wallet = web3.eth.accounts.wallet.add(ganachePrivateKey)
console.log(`Loaded wallet ${wallet.address}`)

// 合约
const abi = Token.abi
const contractAddress = '0x4EB08c5713C47A2a463Abf0cD5B6a28514c567Ad'

// 转账方法
async function transfer(contractAddress, to, amount) {
  var contractInstance = new web3.eth.Contract(abi, contractAddress)

  await contractInstance.methods
    .transfer(to, amount)
    .send({ from: wallet.address, gas: 1500000 }, function (error, result) {
      if (!error) console.log('transfer hash:', result)
      else console.log(error)
    })
}

transfer(contractAddress, userAddress, 10)
